package com.kymatrix.kycarbon.xsmd.kbpm.server.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kymatrix.kycarbon.xsmd.common.springsecurity.SecurityUtils;
import com.kymatrix.kycarbon.xsmd.kbpm.server.base.CommonPage;
import com.kymatrix.kycarbon.xsmd.kbpm.server.core.dto.ProcessSurrogatePageParam;
import com.kymatrix.kycarbon.xsmd.kbpm.server.core.dto.ProcessSurrogateParam;
import com.kymatrix.kycarbon.xsmd.kbpm.server.core.service.ProcessSurrogateService;
import com.kymatrix.kycarbon.xsmd.kbpm.server.core.vo.ProcessSurrogateEntityVO;
import com.kymatrix.kycarbon.xsmd.kbpm.server.dao.ProcessSurrogateMapper;
import com.kymatrix.kycarbon.xsmd.kbpm.server.model.entity.ProcessSurrogateEntity;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * <p>
 * 流程委托代理 服务实现类
 * </p>
 *
 * @author mldong
 * @since 2023-12-06
 */
@Service
public class ProcessSurrogateServiceImpl extends ServiceImpl<ProcessSurrogateMapper, ProcessSurrogateEntity> implements ProcessSurrogateService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(ProcessSurrogateParam param) {
        param.setId(null);
        param.setOperator(SecurityUtils.getLoginUserId());
        ProcessSurrogateEntity processSurrogateEntity = new ProcessSurrogateEntity();
        BeanUtil.copyProperties(param, processSurrogateEntity);
        return super.save(processSurrogateEntity);
    }

    @Override
    public boolean update(ProcessSurrogateParam param) {
        ProcessSurrogateEntity processSurrogateEntity = new ProcessSurrogateEntity();
        param.setOperator(SecurityUtils.getLoginUserId());
        BeanUtil.copyProperties(param, processSurrogateEntity);
        return super.updateById(processSurrogateEntity);
    }

    @Override
    public CommonPage<ProcessSurrogateEntityVO> page(ProcessSurrogatePageParam param) {
        IPage<ProcessSurrogateEntityVO> page = param.buildMpPage();
        QueryWrapper queryWrapper = param.buildQueryWrapper();
        queryWrapper.eq("t.operator", SecurityUtils.getLoginUserId());
        List<ProcessSurrogateEntityVO> list = baseMapper.selectCustom(page, queryWrapper);
        page.setRecords(list);
        return CommonPage.toPage(page);
    }
    @Override
    public ProcessSurrogateEntityVO findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public String getSurrogate(String operator, String processName) {
        List<ProcessSurrogateEntity> processSurrogateEntityList = baseMapper.selectList(
                Wrappers.lambdaQuery(ProcessSurrogateEntity.class)
                        .eq(ProcessSurrogateEntity::getOperator,operator)
                        .eq(ProcessSurrogateEntity::getEnabled,1)
                        .orderByDesc(ProcessSurrogateEntity::getId)
        );
        for (ProcessSurrogateEntity processSurrogateEntity : processSurrogateEntityList) {
            // 为空表示全部，优先级最高，直接返回
            if(StrUtil.isEmpty(processSurrogateEntity.getProcessName())) {
                // 开始时间判断
                if(processSurrogateEntity.getStartTime()!= null && processSurrogateEntity.getStartTime().getTime() > System.currentTimeMillis()) {
                    continue;
                }
                // 结束时间判断
                if(processSurrogateEntity.getEndTime()!= null && processSurrogateEntity.getEndTime().getTime() < System.currentTimeMillis()) {
                    continue;
                }
                return processSurrogateEntity.getSurrogate();
            }
        }
        // 取满足条件的最新一条
        return processSurrogateEntityList.stream().filter(processSurrogate -> {
            // 只查询流程名称一样的
            return ObjectUtil.equals(processSurrogate.getProcessName(),processName);
        }).filter(processSurrogate -> {
            // 开始时间判断
            if(processSurrogate.getStartTime()!= null && processSurrogate.getStartTime().getTime() > System.currentTimeMillis()) {
                return false;
            }
            // 结束时间判断
            if(processSurrogate.getEndTime()!= null && processSurrogate.getEndTime().getTime() < System.currentTimeMillis()) {
                return false;
            }
            return true;
        }).map(processSurrogate -> processSurrogate.getSurrogate()).findFirst().orElse(null);
    }
}
